﻿using System.Linq;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using AstekServis.Web.Data;
using System;
using System.Collections.Generic;

namespace AstekServis.Views
{
    public partial class EvrakTasarim : ChildWindow
    {
        public TasarimTipleri TasarimTipi { get; set; }
        public enum TasarimTipleri : byte
        {
            HizmetFaturasi = 0
        }

        AsTekDomainContext context = new AsTekDomainContext();

        public EvrakTasarim()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(EvrakTasarim_Loaded);
        }

        void EvrakTasarim_Loaded(object sender, RoutedEventArgs e)
        {
            LoadHizmetKartlari();
        }

        private void FindControlBySetValue(string kontrolAdi, int value)
        {
            foreach (var q in grid1.Children)
            {
                if (q.GetType() == typeof(TextBox))
                {
                    TextBox txt = q as TextBox;
                    if (txt.Name == kontrolAdi)
                    {
                        txt.Text = value.ToString();
                        break;
                    }
                }
            }
        }
        private string FindControlByGetValue(string kontrolAdi)
        {
            string value = "";
            foreach (var q in grid1.Children)
            {
                if (q.GetType() == typeof(TextBox))
                {
                    TextBox txt = q as TextBox;
                    if (txt.Name == kontrolAdi)
                    {
                        if (!string.IsNullOrEmpty(txt.Text))
                            value = txt.Text;
                        else
                            value = "0";
                        break;
                    }
                }
                else if (q.GetType() == typeof(TextBlock))
                {
                    TextBlock txt = q as TextBlock;
                    if (txt.Name == kontrolAdi)
                    {
                        value = txt.Text;
                        break;
                    }
                }
            }
            return value;
        }

        private void LoadHizmetKartlari()
        {
            EntityQuery<FormTasarimi> qry = context.GetFormTasarimlariQuery().Where(x => x.Tip == 0);
            LoadOperation<FormTasarimi> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<FormTasarimi> op = ss as LoadOperation<FormTasarimi>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    var liste = op.Entities.ToList();
                    foreach (var item in liste)
                    {
                        FindControlBySetValue("str" + item.AlanKodu.ToString(), item.Satir);
                        FindControlBySetValue("stn" + item.AlanKodu.ToString(), item.Sutun);
                        FindControlBySetValue("gen" + item.AlanKodu.ToString(), item.Genislik);
                    }
                }
            };
        }


        private void SaveServisSorunTanimlari()
        {
            LoadOperation<FormTasarimi> operation = context.Load(context.GetFormTasarimlariQuery().Where(x => x.Tip == 0));
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<FormTasarimi> op = ss as LoadOperation<FormTasarimi>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    var liste = op.Entities.ToList();
                    foreach (var item in liste)
                    {
                        context.FormTasarimis.Remove(item);
                    }
                    context.SubmitChanges(so =>
                    {
                        if (so.HasError)
                            MessageBox.Show("Hata:\r\n" + so.Error.Message);
                        else
                        {
                            for (int i = 0; i < 18; i++)
                            {
                                FormTasarimi frm = new FormTasarimi();
                                frm.Tip = (byte)TasarimTipi;
                                frm.AlanAdi = FindControlByGetValue("txt" + i.ToString());
                                frm.AlanKodu = (byte)i;
                                frm.Genislik = Convert.ToInt32(FindControlByGetValue("gen" + i.ToString()));
                                frm.Satir = Convert.ToInt32(FindControlByGetValue("str" + i.ToString()));
                                frm.Sutun = Convert.ToInt32(FindControlByGetValue("stn" + i.ToString()));
                                context.FormTasarimis.Add(frm);
                            }

                            context.SubmitChanges(so1 =>
                            {
                                if (so1.HasError)
                                    MessageBox.Show("Hata:\r\n" + so1.Error.Message);
                                else
                                {
                                    this.Close();
                                }
                            }, null);

                        }
                    }, null);
                }
            };
        }

        private void btnKaydet_Click(object sender, RoutedEventArgs e)
        {
            if (SatirGenislikKontrol())
                SaveServisSorunTanimlari();
        }

        private void AltSatirKontrol(object sender, RoutedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt != null)
            {
                str11.Text = txt.Text;
                str12.Text = txt.Text;
                str13.Text = txt.Text;
                str14.Text = txt.Text;
                str15.Text = txt.Text;
            }
        }


        private bool SatirGenislikKontrol()
        {
            bool sonuc = true;
            Dictionary<int, int> dic = new Dictionary<int, int>();
            int satir;
            for (int i = 0; i < 18; i++)
            {
                satir = Convert.ToInt32(FindControlByGetValue("str" + i.ToString()));
                if (dic.ContainsKey(satir))
                {
                    dic[satir] += Convert.ToInt32(FindControlByGetValue("gen" + i.ToString()));
                }
                else
                {
                    dic.Add(satir, Convert.ToInt32(FindControlByGetValue("gen" + i.ToString())));
                }
            }

            foreach (var item in dic)
            {
                if (item.Value > 40)
                {
                    sonuc = false;
                    MessageBox.Show(item.Key.ToString() + " no'lu satır alan genişliği 40 haneyi aşmıştır !");
                }
            }
            return sonuc;
        }


    }
}

